package com.lrh.java8;

import javafx.scene.chart.Chart;

import javax.sound.midi.Soundbank;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class MethodReference {
    public static void main(String[] args) {
        Consumer<String> consumer = (s) -> System.out.println(s);
        useConsumer(consumer, "Hello LRH");

        useConsumer(s -> System.out.println(s), "Hello DD");

        useConsumer(System.out::println, "Hello JJ");

        System.out.println("------------------------");

        int value = Integer.parseInt("123");
        Function<String, Integer> f = Integer::parseInt;
        int value2 = f.apply("123");
        System.out.println(value);
        System.out.println(value2);

        System.out.println("------------------------");

        BiFunction<String, Integer, Character> f2 = String::charAt;
        char c = f2.apply("123434", 2);
        System.out.println(c);

        System.out.println("------------------------");

        String str = new String("7687854");
        Function<Integer, Character> f3 = str::charAt;
        char c3 = f3.apply(2);
        System.out.println(c3);

        System.out.println("------------------------");
        Supplier<String> supplier = String::new;
        String s = supplier.get();
        System.out.println(s);

        System.out.println("------------------------");

        BiFunction<String, Long, Apple> appleFuntion = Apple::new;
        Apple apple = appleFuntion.apply("red", 100L);
        System.out.println(apple);

        System.out.println("------------2------------");

        List<Apple> list = Arrays.asList(new Apple("Green", 110),new Apple("abc", 123), new Apple("red", 59));
        System.out.println(list);
        list.sort((a1, a2) -> a1.getColor().compareTo(a2.getColor()));
        System.out.println(list);
        System.out.println("------------3------------");
        list.stream().forEach( a -> System.out.println(a));
        System.out.println("------------4------------");
        list.stream().forEach( System.out::println);

        System.out.println("------------------------");

        List<Apple> list2 = Arrays.asList(new Apple("Green", 110),new Apple("red", 123), new Apple("abc", 59));
        list2.sort(Comparator.comparing(Apple::getColor));
        System.out.println(list2);

        Function<Apple, String> ffff = Apple::getColor;
        System.out.println(ffff.apply(new Apple("dd", 23)));
    }

    private static <T> void useConsumer(Consumer<T> consumer, T t){
        consumer.accept(t);
        consumer.accept(t);
    }
}
